/**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
* File Name          : TimerInit.c
* Author             : Fortiortech  Appliction Team
* Version            : V1.0
* Date               : 10-Apr-2017
* Description        : This file contains Timer Initial function used for Motor Control.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/


/* Includes -------------------------------------------------------------------------------------*/
#include <FU68xx_2.h>
#include <Myproject.h>
//extern uint8  idata MotorDirection;


///*-------------------------------------------------------------------------------------------------
//	Function Name :	void TIM0_Init(void)
//	Description   :	TIM0初始化配置
//	Input         :	无
//  Output				:	无
//-------------------------------------------------------------------------------------------------*/
//void TIM0_Init(void)
//{


//}

/*-------------------------------------------------------------------------------------------------
	Function Name :	void TIM1_Init(void)
	Description   :	TIM1初始化配置
	Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/
void TIM1_Init(void)
{
	TIM1__UIGN = 0;
  TIM1_KR = 0x6000; 
  TIM1_KF = 0x4000;
  SetBit(TIM1_CR3, T1AFL);
	
	ClrBit(TIM1_CR3,T1PSC2);
	SetBit(TIM1_CR3,T1PSC1);
	ClrBit(TIM1_CR3,T1PSC0);
	
  ClrBit(TIM1_CR0, T1CFLT1); 
  ClrBit(TIM1_CR0, T1CFLT0);
	
	ClrBit(TIM1_CR1, T1BAPE);
  ClrBit(TIM1_CR0, T1FORC); 

	SetBit(TIM1_CR2, T1BRS);
	
  ClrBit(TIM1_CR3, T1TIS1);
  ClrBit(TIM1_CR3, T1TIS0);
 
  SetBit(TIM1_CR3, T1INM1);
  SetBit(TIM1_CR3, T1INM0);
 
 /*-------------------------------------------------------------------------------------------------
  屏蔽续流时间（角度）设置
  RUN_ANGLE_MASK    
  -------------------------------------------------------------------------------------------------*/
  ANGLE_MASK(10);

  /*-------------------------------------------------------------------------------------------------
  检测到过零点后的延迟换相时间（角度）设置
  -------------------------------------------------------------------------------------------------*/
 
  ANGLE_DELAY(30);
 
  SetBit(TIM1_CR0, T1RWEN); 
  ClrBit(TIM1_CR0, T1RCEN);

	
  TIM1_CR4 = 0x07; 
	TIM1_SR &= 0x00;
	
  ClrBit(TIM1_IER, T1ADIE);                
  ClrBit(TIM1_IER, T1BOIE);                     
  ClrBit(TIM1_IER, T1ROIE);                      
  ClrBit(TIM1_IER, T1WTIE);                    
  ClrBit(TIM1_IER, T1PDIE);                        
  ClrBit(TIM1_IER, T1BDIE);      


		/**------------------------------------------------------
	使能比较器CMP0,CMP1,CMP2和ADC在pwm on/off采样功能
	00：在on和off均采样，没有延迟采样开启
	01：只在off采样，根据CMP_SAMR延迟采样开启
	10：只在on采样，根据CMP_SAMR延迟采样开启
	11：在on和off均采样，根据CMP_SAMR延迟采样开启
	---------------------------------------------------------**/
	SetBit(CMP_CR3, SAMSEL1);
	SetBit(CMP_CR3, SAMSEL0);
	
	
  PTIM11 = 1;
  PTIM10 = 0;         

	TIM1_DBR1 = UH_VLA1 + CMP_UVW_UPDO;
	TIM1_DBR2 = UH_WLA1 + CMP_UVW_UPDO;
	TIM1_DBR3 = VH_WLA1 + CMP_UVW_UPDO;
	TIM1_DBR4 = VH_ULA1 + CMP_UVW_UPDO;
	TIM1_DBR5 = WH_ULA1 + CMP_UVW_UPDO;
	TIM1_DBR6 = WH_VLA1 + CMP_UVW_UPDO;
	TIM1_DBR7 = CMP_UVW_UPDOOFF;
  TIM1__BCCR = 65535;
  TIM1__BARR = 20000;
  TIM1__BCNTR = 0;
  TIM1__RARR = 65535;
  TIM1__RCNTR = 0;	
	
  ClrBit(TIM1_IER, T1UPD);
  SetBit(TIM1_CR0, T1BCEN);
	
	
  ClrBit(TIM1_CR0, T1OPS1);
  ClrBit(TIM1_CR0, T1OPS0);
}


void TIM2_Init(void)
{
	ClrBit(TIM2_CR1,T2EN);
	ClrBit(PH_SEL, T2SEL);
	
	ClrBit(TIM2_CR0,T2PSC2);
	SetBit(TIM2_CR0,T2PSC1);
	ClrBit(TIM2_CR0,T2PSC0);
	
  ClrBit(TIM2_CR0,T2MOD1);
  SetBit(TIM2_CR0,T2MOD0);
	
		
	ClrBit(TIM2_CR0,T2IRE);   //使能匹配中断
	ClrBit(TIM2_CR1,T2IR);    //清匹配中断标记	
	SetBit(TIM2_CR1,T2IFE);   //使能溢出中断
	ClrBit(TIM2_CR1,T2IF);    //清溢出中断标记	
	
	TIM2__ARR  = 0;
	TIM2__DR   = 0;	
  TIM2__CNTR = 0;
	
	PTIM21 = 1;
  PTIM20 = 0;
  EA = 1;
	ClrBit(TIM2_CR1,T2EN);	
	
}

void Time3_BMEF_Init(void)
{
	/*-------------------------------------------------------------------------------------------------
  先停止计数，配置完寄存器后，最后启动计数
-------------------------------------------------------------------------------------------------*/
	ClrBit(TIM3_CR1, T3EN);			                           // 0，停止计数；1,使能计数
    ClrBit(PH_SEL, T3SEL);				//0-不连接到端口GP11；1-连接到端口GP11
  //   SetBit(PH_SEL, T3SEL);
/*-------------------------------------------------------------------------------------------------
	时钟分频设置(T2PSC)
  000:cpuclk(24MHz)			001:cpuclk/2^1(12MHz)	010:cpuclk/2^2(6MHz)	011:cpuclk/2^3(3MHz)
  100:cpuclk/2^4(1.5MHz)	101:cpuclk/2^5(750KHz)	110:cpuclk/2^6(375KHz)	111:cpuclk/2^7(187.5KHz)
-------------------------------------------------------------------------------------------------*/
	SetReg(TIM3_CR0, T3PSC0 | T3PSC1 | T3PSC2, T3PSC0 | T3PSC1 | T3PSC2);
	/*-------------------------------------------------------------------------------------------------
	/模式选择
	T2MODE1，T2MODE0
	00--输入Timer模式；01--输出模式
	10--输入Count模式；11--QEP或者RSD模式
-------------------------------------------------------------------------------------------------*/
	
  SetBit(TIM3_CR0, T3MOD);	
	
//  SetBit(TIM3_CR0, T3OCM);

//  ClrBit(TIM3_CR0, T3OPM);// added by t 
	/*-------------------------------------------------------------------------------------------------
  清除中断标志位
	禁止PWM周期检测中断使能
-------------------------------------------------------------------------------------------------*/
//	ClrBit(TIM3_CR0, T3CES);		                               // 清零脉冲计数器不使能
	
	ClrBit(TIM3_CR1, T3IR | T3IF | T3IP);		           // 清零中断标志位

/*-------------------------------------------------------------------------------------------------
  配置周期值、比较值、计数值
	禁止PWM周期检测中断使能
	使能计数器上溢中断使能
-------------------------------------------------------------------------------------------------*/
	TIM3__ARR =  60000;								                         // TIM2 Period = 0.32s
	TIM3__DR = TIM3__ARR;
  TIM3__CNTR = 0;
/*-----------启动计数------------------------------------------------*/
	SetBit(TIM3_CR1, T3EN);				                       //启动计数
}

/*-------------------------------------------------------------------------------------------------
	Function Name :	void TIM_1ms_Init(void)
	Description   :	TIM1MS初始化配置
	Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/

void TIM_1ms_Init(void)
{
	//SYST_ARR = 24000;     //计算公式：24M/SYST_ARR；例 24M/24000=1000Hz
	PTIM4S1 = 0;
	PTIM4S0 = 1;										                   // TIM4/5中断优先级别为1
	EA = 1;
	SetBit(DRV_SR, SYSTIE);		                     // 使能1ms定时中断
}
